package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    @GetMapping("list")
    public R list(){
        Long id = BaseContext.getThreadLocal();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, id);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("add")
    public R add(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseContext.getThreadLocal());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart != null) {
            //如果已经存在，就在原来数量基础上加一
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartService.updateById(cart);
        } else {
            //如果不存在，则添加到购物车，数量默认就是一
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setUserId(BaseContext.getThreadLocal());//设置用户id
            shoppingCartService.save(shoppingCart);
        }
        return R.success("添加购物车成功");
    }

    @DeleteMapping("clean")
    public R clean(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = Wrappers.lambdaQuery(ShoppingCart.class)
                .eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        shoppingCartService.remove(queryWrapper);
        return R.success("删除成功");
    }

    @PostMapping("sub")
    public R sub(@RequestBody ShoppingCart shoppingCart){
        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(shoppingCart.getDishId() != null, "dish_id", shoppingCart.getDishId());
        queryWrapper.eq(shoppingCart.getSetmealId() != null, "setmeal_id", shoppingCart.getSetmealId());
        queryWrapper.eq("user_id", BaseContext.getThreadLocal());

        ShoppingCart cart = shoppingCartService.getOne(queryWrapper);

        if (cart.getNumber() > 1) {
            //如果已经存在，就在原来数量基础上减一
            cart.setNumber(cart.getNumber() - 1);
            shoppingCartService.updateById(cart);
        } else {
            //如果数量是一，就直接删除
            shoppingCartService.remove(queryWrapper);
        }


        return R.success("删除成功");
    }

}
